package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.come.entity.Goodstable;
import org.come.model.Shop;
import org.come.mouslisten.ShopFyMouslisten;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.Util;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.LotteryBtn;
import com.tool.tcpimg.UIUtils;



public class RewardHallJpanel extends JPanel {

    private List<Goodstable> listGoods;// 商品列表
    // 翻页按钮
    private List<JLabel> pages;// 商品列表
    private int index = 0;// 判断是在第几页
    private Shop shop;// 当前要购买的物品
    private int xz = -1;// 当前要购买的物品位置

    private BigDecimal Unitprice,// 每次抽奖所需的功绩
            Totalsum,// 总功绩
            lotteryNum;// 可抽奖次数
    private LotteryBtn btnlottery;// 抽奖按钮

    public JLabel[] GoodsListLabel = new JLabel[24];// 用来存放24个物品表格

    private int Flag = 0;
    private int count = 1;

    public RewardHallJpanel() throws Exception {

        this.setPreferredSize(new Dimension(380, 387));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 59);
        offBtn.setBounds(380 - 37, 10, 25, 25);
        this.add(offBtn);

        Unitprice = new BigDecimal(50);
        Totalsum = new BigDecimal(0);
        // ImageMixDeal.userimg.getLoginResult().getScoretype("帮派积分")!=null?ImageMixDeal.userimg.getLoginResult().getScoretype("帮派积分"):new
        // BigDecimal(0);
        int val = Totalsum.divide(Unitprice).intValue();
        lotteryNum = new BigDecimal(val);

        btnlottery = new LotteryBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "抽奖", this);
        btnlottery.setBounds(190, 336, 59, 24);
        this.add(btnlottery);

        // 存放物品的表格1~24
        for (int i = 0; i < 24; i++) {

            GoodsListLabel[i] = new JLabel();
            // shopMouslistens[i]=new ShopMouslisten(i,this);
            // GoodsListLabel[i].addMouseListener(shopMouslistens[i]);
            if ((Flag < 6) && (count == 1)) {
                GoodsListLabel[i].setBounds(49 + Flag * 51, 33 + (count - 1) * 51, 48, 48);
                Flag++;// 用来标记行
                this.add(GoodsListLabel[i]);

            }
            if ((Flag < 6) && (count == 2)) {
                GoodsListLabel[i].setBounds(49 + Flag * 51, 33 + (count - 1) * 51, 48, 48);
                Flag++;// 用来标记行
                this.add(GoodsListLabel[i]);
            }
            if ((Flag < 6) && (count == 3)) {
                GoodsListLabel[i].setBounds(49 + Flag * 51, 33 + (count - 1) * 51, 48, 48);
                Flag++;// 用来标记行
                this.add(GoodsListLabel[i]);
            }
            if ((Flag < 6) && (count == 4)) {

                GoodsListLabel[i].setBounds(49 + Flag * 51, 33 + (count - 1) * 51, 48, 48);
                Flag++;// 用来标记行
                this.add(GoodsListLabel[i]);

            } else if (Flag == 6) {
                Flag = 0;
                count++;
            }
        }

        image.setImage(image.getImage().getScaledInstance(38, 20, Image.SCALE_DEFAULT));

    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 赏功堂
        if (icon == null) {
            icon = new ImageIcon("inkImg/old/background/S82.png");
        }

        g.drawImage(icon.getImage(), 0, 0, 380, 387, this);
        Util.drawPrice(g, Unitprice, 165, 272);
        Util.drawPrice(g, lotteryNum, 165, 296);
        Util.drawPrice(g, Totalsum, 165, 321);
    }

    /**
     * 显示对应的商品图片
     * 
     * @return
     * @return
     */
    public void ShoppingsetIcon(int index) {
        this.index = index;
        fysum();
        xz = -1;
        // 清空GoodsListLabel中所以的icon
        for (int i = 0; i < GoodsListLabel.length; i++) {
            GoodsListLabel[i].setIcon(null);
            GoodsListLabel[i].setBorder(null);
        }
        shop = null;
        int size = listGoods.size() > (index + 1) * 24 ? 24 : listGoods.size() - index * 24;
        for (int i = 0; i < size; i++) {
            ImageIcon image = GoodsListFromServerUntil.imgpathAdaptive(listGoods.get(index * 24 + i).getSkin(),49,49);
            GoodsListLabel[i].setIcon(image);
        }
    }

    /**
     * 选中商品
     */
    // public void xuanzhong(Shop yxshop,int shopPlace){
    // if (shop!=null&&yxshop.getShopid()==shop.getShopid()) {
    // //如果这商品之前被点击则购买数量加1
    // int TextNumber=Integer.
    // parseInt(textNumber.getText())+1;//数量
    // if (TextNumber>99) return ;
    // long LabUnitprice=Long.parseLong(shop.getShopprice());//单价
    // textNumber.setText(TextNumber+"");//改变数量
    // // labTotalsum.setText(LabUnitprice*TextNumber+"");//改变总价
    // Totalsum = new BigDecimal((LabUnitprice*TextNumber));//改变总价
    // }else {
    // if (shop!=null) {
    // GoodsListLabel[xz].setBorder(null);
    // }
    // xz=shopPlace;
    // //如果这商品没被点击则初始化购买数量 单价等数据
    // shop=yxshop;
    // textNumber.setText(1+"");//数量
    // // labUnitprice.setText(shop.getShopprice()+"");//单价
    // // labTotalsum.setText(shop.getShopprice()+"");//总价
    // Unitprice = new BigDecimal(shop.getShopprice());//单价
    // Totalsum = new BigDecimal(shop.getShopprice());
    // }
    // }
    /**
     * 初始化按钮个数
     */
    public void fysum() {
        int sum = (listGoods.size() - 1) / 24 + 1;
        if (pages == null) {
            pages = new ArrayList<>();
        }

        for (int i = 0; i < sum; i++) {
            if (i >= 8) {
                break;
            }
            if (i >= pages.size()) {
                if (sum > 8 && i == 0) {
                    pages.add(fyjlb("首页", i));
                } else if (sum > 8 && i == 7) {
                    pages.add(fyjlb("尾页", i));
                } else if (sum > 8) {
                    if (index == 0) {
                        pages.add(fyjlb(i + "", i));
                    } else if (index > sum - 5) {
                        int q = sum - 6;
                        pages.add(fyjlb((i + q) + "", i));
                    } else {
                        pages.add(fyjlb((i + index - 1) + "", i));
                    }
                } else {
                    pages.add(fyjlb((i + 1) + "", i));
                }
            } else {
                if (sum > 8 && i == 0) {
                    pages.get(i).setText("首页");
                } else if (sum > 8 && i == 7) {
                    pages.get(i).setText("尾页");
                } else if (sum > 8) {
                    if (index == 0) {
                        pages.get(i).setText(i + "");
                    } else if (index > sum - 5) {
                        int q = sum - 6;
                        pages.get(i).setText((i + q) + "");
                    } else {
                        pages.get(i).setText((i + index - 1) + "");
                    }
                } else {
                    pages.get(i).setText(i + 1 + "");
                }

            }
        }
        if (pages.size() > sum) {
            for (int i = pages.size() - 1; i >= sum; i--) {
                remove(pages.get(i));
                pages.remove(i);
            }
        }
    }

    private ImageIcon image = new ImageIcon("skin/8100226/button/B162.png");

    /**
     * 初始化第i个按钮
     */
    public JLabel fyjlb(String text, int i) {
        JLabel lab = new JLabel();
        lab.setBounds(47 + i * 41, 238, 39, 23);
        lab.setIcon(image);
        lab.setBackground(new Color(0, 0, 0, 0));
        lab.setText(text);
        lab.setForeground(Color.WHITE);
        lab.setHorizontalTextPosition(SwingConstants.CENTER);
        lab.setFont(UIUtils.TEXT_FONT1);
        lab.addMouseListener(new ShopFyMouslisten(lab, null, this));
        this.add(lab);
        return lab;
    }

    /**
     * 画出预选边框
     * 
     * @return
     */
    public void PaintingText(int shopPlace) {
        GoodsListLabel[shopPlace].setBorder(BorderFactory.createLineBorder(Color.red));
    }

    /**
     * 清除预选边框
     * 
     * @return
     */
    public void ClearText(int shopPlace) {
        if (xz != shopPlace) {
            GoodsListLabel[shopPlace]

            .setBorder(null);
        }

    }

    /**
     * 打开商店
     * 
     * @return
     */
    public void showshop(List<Goodstable> listGoods) { // String type
        this.listGoods = listGoods;
        if (this.listGoods == null) {
            this.listGoods = new ArrayList<>();
        }
        // 画出所有的物品
        ShoppingsetIcon(0);

    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public BigDecimal getUnitprice() {
        return Unitprice;
    }

    public void setUnitprice(BigDecimal unitprice) {
        Unitprice = unitprice;
    }

    public BigDecimal getTotalsum() {
        return Totalsum;
    }

    public void setTotalsum(BigDecimal totalsum) {
        Totalsum = totalsum;
    }

    public Shop getShop() {
        return shop;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    public BigDecimal getLotteryNum() {
        return lotteryNum;
    }

    public void setLotteryNum(BigDecimal lotteryNum) {
        this.lotteryNum = lotteryNum;
    }

    public LotteryBtn getBtnlottery() {
        return btnlottery;
    }

    public void setBtnlottery(LotteryBtn btnlottery) {
        this.btnlottery = btnlottery;
    }

    public JLabel[] getGoodsListLabel() {
        return GoodsListLabel;
    }

    public void setGoodsListLabel(JLabel[] goodsListLabel) {
        GoodsListLabel = goodsListLabel;
    }

    public List<Goodstable> getListGoods() {
        return listGoods;
    }

    public void setListGoods(List<Goodstable> listGoods) {
        this.listGoods = listGoods;
    }

}
